package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdaT;
import cn.e4j.modular.crm.mapper.GwdaTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-02-02
 */
@Service
public class GwdaTServiceImpl extends ServiceImpl<GwdaTMapper, GwdaT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        List<Map<String, Object>> gwdaDtos = this.baseMapper.listPage(page, dto);
        return gwdaDtos;
    }

    @Transactional
    public void add(GwdaT gwdaT) {
        ShiroUser user = ShiroKit.getUser();
//        Dto dto = this.baseMapper.getMaxGwda001();
        Dto dto = InvokeUtil.invokeBeanMethod("gwdaTMapper,getMaxGwda001");
        gwdaT.setGWDA001(dto.getBigDecimal("GWDA001"));
        gwdaT.setGWDAENT(user.getDBC());
        gwdaT.insert();
    }

    @Transactional
    public void edit(GwdaT gwdaT) {
        ShiroUser user = ShiroKit.getUser();
        UpdateWrapper<GwdaT> gwdaTUpdateWrapper = new UpdateWrapper<>();
        gwdaTUpdateWrapper.eq("GWDA001", gwdaT.getGWDA001());
        gwdaTUpdateWrapper.eq("GWDAENT", gwdaT.getGWDAENT());
        gwdaT.setGWDADATE(LocalDate.now());
        gwdaT.setGWDATIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwdaT.update(gwdaTUpdateWrapper);
    }

    @Transactional
    public void delete(GwdaT gwdaT) {
        UpdateWrapper<GwdaT> gwdaTUpdateWrapper = new UpdateWrapper<>();
        gwdaTUpdateWrapper.eq("GWDAENT", gwdaT.getGWDAENT());
        gwdaTUpdateWrapper.eq("GWDA001", gwdaT.getGWDA001());
        gwdaT.delete(gwdaTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwdaT> gwdaTList) {
        for (GwdaT gwdaT : gwdaTList) {
            UpdateWrapper<GwdaT> gwdaTUpdateWrapper = new UpdateWrapper<>();
            gwdaTUpdateWrapper.eq("GWDA001", gwdaT.getGWDA001());
            gwdaTUpdateWrapper.eq("GWDAENT", gwdaT.getGWDAENT());
            gwdaT.delete(gwdaTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public Dto checkGwda003(Dto dto) {
        Dto gwamDtos = this.baseMapper.checkGwda003(dto);
        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public Dto checkGwda007(Dto dto) {
        Dto gwamDtos = this.baseMapper.checkGwda007(dto);
        return gwamDtos;
    }


    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwakData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwakDtos = this.baseMapper.getGwakData(page, dto);
        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwamDtos = this.baseMapper.getGwamData(page, dto);
        return gwamDtos;
    }

    @Transactional
    public void addData(List<GwdaT> gwdaTList) {
        ShiroUser user = ShiroKit.getUser();
        Dto dto = this.baseMapper.getMaxGwda001();
        BigDecimal GWDA001 = dto.getBigDecimal("GWDA001");
        for (GwdaT gwdaT : gwdaTList) {
            gwdaT.setGWDA001(GWDA001);
            gwdaT.setGWDAENT(user.getDBC());
            gwdaT.setGWDAACTI("Y");
            gwdaT.insert();
            GWDA001 = GWDA001.add(new BigDecimal(1));
        }
    }
}
